package com.sc.justification.dao;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

@SuppressWarnings("all")
public class GenericDao<T> {
	EntityManagerFactory factory = Persistence
			.createEntityManagerFactory("justification");
	EntityManager em = factory.createEntityManager();
	private Class entityClass;

	public GenericDao(Class entityClass) {
		this.entityClass = entityClass;
	}

	public List<T> getAll() {
		return em.createQuery(
				"select o from " + entityClass.getSimpleName() + " o",
				entityClass).getResultList();
	}
	public List<T> query(String sql){
		return em.createQuery(sql).getResultList();
	}
	public void save(T o) {
		em.getTransaction().begin();
		em.persist(o);
		em.getTransaction().commit();
	}

	public void update(T o) {
		em.getTransaction().begin();
		em.merge(o);
		em.getTransaction().commit();
	}
	public void delete(T o){
		em.getTransaction().begin();
		o=em.merge(o);
		em.remove(o);
		em.getTransaction().commit();
	}
	public void refresh(T o) {
		em.refresh(o);
	}
}
